layui.use(['admin', 'form'], function () {
    let $ = layui.$
        , setter = layui.setter
        , admin = layui.admin
        , form = layui.form
        , router = layui.router()
        , search = router.search
        , laytpl = layui.laytpl;

    form.render(null, 'loginPage');

    let field = {};
    admin.req({
        url: setter.request.domainName + '/admin/Index/login'
        , data: field
        , type: 'get'
        , loading: true // 是否开启加载框
        , is_message: false
        , success: function (res) {
            if(res.code === 1){
                const dataName = setter.response.dataName; // 数据详情的字段名称
                let siteName = res[dataName].siteConfig.site_name;
                $("#siteName").html(siteName);
                $("#siteTitle").html(siteName);
                let getTpl = loginTpl.innerHTML;
                let html = laytpl(getTpl).render({
                    captcha: res[dataName].captcha,
                });
                $("#loginView").html(html);
            }
            form.render(); //重载表格
        }
    })

    const $body = $('body');
    //提交
    form.on('submit(LAYSubmitLogin)', function (obj) {
        //请求登入接口
        admin.req({
            url: setter.request.domainName + '/admin/Index/login'
            , data: obj.field
            ,type: 'POST'
            ,loading: true // 是否开启加载框
            , done: function (res) {
                //请求成功后，写入 access_token
                layui.data(setter.tableName, {
                    key: setter.request.tokenName
                    , value: res.data.userinfo.token
                });
                //请求成功后，写入 刷新 token
                layui.data(setter.tableName, {
                    key: setter.request.refreshTokenName
                    , value: res.data.userinfo.refreshToken
                });

                layui.data(setter.tableName, {
                    key: setter.request.userInfo
                    , value: res.data.userinfo
                });
                search.redirect = '../web/index.html'
                //登入成功的提示与跳转
                admin.message({
                    title: '即将跳转'
                    ,offset: '15px'
                    ,icon: 1
                    ,time: 1000
                    ,callback: function (){
                        window.location.href = search.redirect ? decodeURIComponent(search.redirect) : '/'
                    }
                })
            }
        });
    });

    //更换图形验证码
    $body.on('click', '#LAYGetVercode', function () {
        this.src = layui.setter.request.domainName + '/api/Common/captcha?id=' + generateMixed(16) + '&server=true'
    });

    // 生成验证码ID
    window.generateMixed = function(n){
        const chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
        let res = "";
        for(let i = 0; i < n ; i ++) {
            const id = Math.ceil(Math.random() * 35);
            res += chars[id];
        }
        setTimeout(function (){
            $body.find("#captcha_id").val(res);
        }, 200)
        return res;
    }
});
